subroutine MinMaxWrite()
use Data_cfoam, only: Unew,pnew,Tnew,Rhonew, Usnew,psnew,Tsnew,Rhosnew, rank
use Data_Mesh, only: nCells,nFaces
!use DataPerfectGas
implicit none
    
    real(8) :: Pmin,Pmax, Umin,Umax, Vmin,Vmax, Wmin,Wmax, RhoMin,RhoMax, Tmin,Tmax
    integer :: iFace

    Pmin = minVal(pnew(1:nCells))
    Pmax = maxVal(pnew(1:nCells))
    Umin = minVal(Unew(1,1:nCells))
    Umax = maxVal(Unew(1,1:nCells))
    Vmin = minVal(Unew(2,1:nCells))
    Vmax = maxVal(Unew(2,1:nCells))
    Wmin = minVal(Unew(3,1:nCells))
    Wmax = maxVal(Unew(3,1:nCells))
    RhoMin = minVal(Rhonew(1:nCells))
    RhoMax = maxVal(Rhonew(1:nCells))
    Tmin = minVal(Tnew(1:nCells))
    Tmax = maxVal(Tnew(1:nCells))

    call MinMaxFromAllProcs()

    if(rank.eq.0)then
        write(*,*) ' '
        write(*,'(a65)') '******** Cell Statistics ****************************************'

        call WriteStatisticVals()
    end if
    
    Pmin = minVal(psnew(1:nFaces))
    Pmax = maxVal(psnew(1:nFaces))
    Umin = minVal(Usnew(1,1:nFaces))
    Umax = maxVal(Usnew(1,1:nFaces))
    Vmin = minVal(Usnew(2,1:nFaces))
    Vmax = maxVal(Usnew(2,1:nFaces))
    Wmin = minVal(Usnew(3,1:nFaces))
    Wmax = maxVal(Usnew(3,1:nFaces))
    RhoMin = minVal(Rhosnew(1:nFaces))
    RhoMax = maxVal(Rhosnew(1:nFaces))
    Tmin = minVal(Tsnew(1:nFaces))
    Tmax = maxVal(Tsnew(1:nFaces))

    call MinMaxFromAllProcs()

    if(rank.eq.0)then
        write(*,*) ' '
        write(*,'(a65)') '******** Face Statistics ****************************************'

        call WriteStatisticVals()

        write(*,'(a65)') '*****************************************************************'
        write(*,*) ' '
    end if

contains

    subroutine MinMaxFromAllProcs()
    use Data_cfoam, only: nump, ierr
    implicit none
        include 'mpif.h'
        
        real(8) :: a

        if (nump.gt.1) then
            call MPI_ALLREDUCE(Pmin,a,1,MPI_DOUBLE_PRECISION,MPI_MIN,MPI_COMM_WORLD,ierr)
            Pmin=a
            call MPI_ALLREDUCE(Pmax,a,1,MPI_DOUBLE_PRECISION,MPI_MAX,MPI_COMM_WORLD,ierr)
            Pmax=a

            call MPI_ALLREDUCE(Umin,a,1,MPI_DOUBLE_PRECISION,MPI_MIN,MPI_COMM_WORLD,ierr)
            Umin=a
            call MPI_ALLREDUCE(Umax,a,1,MPI_DOUBLE_PRECISION,MPI_MAX,MPI_COMM_WORLD,ierr)
            Umax=a
            
            call MPI_ALLREDUCE(Vmin,a,1,MPI_DOUBLE_PRECISION,MPI_MIN,MPI_COMM_WORLD,ierr)
            Vmin=a
            call MPI_ALLREDUCE(Vmax,a,1,MPI_DOUBLE_PRECISION,MPI_MAX,MPI_COMM_WORLD,ierr)
            Vmax=a
            
            call MPI_ALLREDUCE(Wmin,a,1,MPI_DOUBLE_PRECISION,MPI_MIN,MPI_COMM_WORLD,ierr)
            Wmin=a
            call MPI_ALLREDUCE(Wmax,a,1,MPI_DOUBLE_PRECISION,MPI_MAX,MPI_COMM_WORLD,ierr)
            Wmax=a

            call MPI_ALLREDUCE(RhoMin,a,1,MPI_DOUBLE_PRECISION,MPI_MIN,MPI_COMM_WORLD,ierr)
            RhoMin=a
            call MPI_ALLREDUCE(RhoMax,a,1,MPI_DOUBLE_PRECISION,MPI_MAX,MPI_COMM_WORLD,ierr)
            RhoMax=a

            call MPI_ALLREDUCE(Tmin,a,1,MPI_DOUBLE_PRECISION,MPI_MIN,MPI_COMM_WORLD,ierr)
            Tmin=a
            call MPI_ALLREDUCE(Tmax,a,1,MPI_DOUBLE_PRECISION,MPI_MAX,MPI_COMM_WORLD,ierr)
            Tmax=a
	    end if

    end subroutine MinMaxFromAllProcs
    
    subroutine WriteStatisticVals()
    implicit none

        write(*,'(a25,2a20)')   '     Variable            ', '     min     ', '     max     '
        write(*,'(a25,2f20.8)') 'Pressure, Pa             ', Pmin, Pmax
        write(*,'(a25,2f20.8)') 'X-comp. of velocity, m/s ', Umin, Umax
        write(*,'(a25,2f20.8)') 'Y-comp. of velocity, m/s ', Vmin, Vmax
        write(*,'(a25,2f20.8)') 'Z-comp. of velocity, m/s ', Wmin, Wmax
        write(*,'(a25,2f20.8)') 'Density, kg/m^3          ', RhoMin, RhoMax
        write(*,'(a25,2f20.8)') 'Temperature, K           ', Tmin, Tmax
!        write(*,*) '************************************************************'

    end subroutine WriteStatisticVals

end subroutine MinMaxWrite

subroutine OneValueMinMax(ValMin,ValMax)
use Data_cfoam, only: nump, ierr
implicit none
    include 'mpif.h'
    
    real(8),intent(inout) :: ValMin,ValMax

    real(8) :: a
    
    if (nump.gt.1) then
        call MPI_ALLREDUCE(ValMin,a,1,MPI_DOUBLE_PRECISION,MPI_MIN,MPI_COMM_WORLD,ierr)
        ValMin = a
        call MPI_ALLREDUCE(ValMax,a,1,MPI_DOUBLE_PRECISION,MPI_MAX,MPI_COMM_WORLD,ierr)
        ValMax = a
    else
        ValMin = ValMin
        ValMax = ValMax
    end if

    return
end subroutine OneValueMinMax